package com.pmt.feedplayer.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.interop.UIKitView
import androidx.compose.ui.unit.dp
import com.pmt.feedplayer.data.VideoItem
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import platform.AVKit.AVPlayerViewController
import platform.AVFoundation.AVPlayer
import platform.AVFoundation.AVPlayerItem
import platform.CoreGraphics.CGRectMake
import platform.Foundation.NSURL
import platform.UIKit.*
import platform.UIKit.enums.UIInterfaceOrientationMaskAll

/**
 * iOS平台的视频播放页面实现
 */
actual @Composable
fun VideoPlayerScreen(
    videoItem: VideoItem,
    onBack: () -> Unit
) {
    // 播放控制器状态
    val (playerViewController, setPlayerViewController) = remember { mutableStateOf<AVPlayerViewController?>(null) }
    
    // 初始化播放器
    DisposableEffect(Unit) {
        val player = AVPlayer()
        val playerVC = AVPlayerViewController().apply {
            this.player = player
            this.showsPlaybackControls = true
            this.allowsPictureInPicturePlayback = true
        }
        
        // 设置播放项
        val url = NSURL(string = videoItem.videoUrl)
        val playerItem = AVPlayerItem(URL = url)
        player.replaceCurrentItemWithPlayerItem(playerItem)
        player.play()
        
        setPlayerViewController(playerVC)
        
        onDispose {
            player.pause()
            player.replaceCurrentItemWithPlayerItem(null)
            setPlayerViewController(null)
        }
    }
    
    // 显示播放器视图
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        playerViewController?.let {\ playerVC ->
            UIKitView(
                factory = { _ ->
                    val view = UIView()
                    // 创建容器视图来托管播放器控制器的视图
                    val containerView = UIView()
                    containerView.frame = CGRectMake(0.0, 0.0, 300.0, 400.0)
                    view.addSubview(containerView)
                    
                    // 在后台线程添加播放器视图
                    dispatch_async(dispatch_get_main_queue()) { _ ->
                        playerVC.view.frame = containerView.bounds
                        containerView.addSubview(playerVC.view)
                    }
                    
                    view
                },
                modifier = Modifier.fillMaxSize()
            )
        }
        
        // 返回按钮（固定在左上角）
        Box(
            modifier = Modifier
                .align(Alignment.TopStart)
                .padding(16.dp)
        ) {
            Button(onClick = onBack) {
                Text("返回")
            }
        }
    }
}

/**
 * iOS平台的dispatch_async函数
 */
private fun dispatch_async(queue: dispatch_queue_t, block: (Any?) -> Unit) {
    val block: dispatch_block_t = { block(null) }
    platform.Dispatch.dispatch_async(queue, block)
}

/**
 * 获取主线程队列
 */
private val dispatch_get_main_queue: dispatch_queue_t
    get() = platform.Dispatch.dispatch_get_main_queue()